package com.myWeb.servlet;

import com.myWeb.beans.User;
import com.myWeb.dao.IUserDao;
import com.myWeb.dao.impl.userDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

//处理登录请求的servlet
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-Type", "text/html;charset=UTF-8");
        System.out.println("LoginServlet");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        IUserDao userDao = new userDao();
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        try {
            System.out.println(userDao.canLogin(user));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            if(userDao.canLogin(user)){
                req.getRequestDispatcher("home.jsp").forward(req,resp);
            }else{
                resp.getWriter().write("<script>alert('用户名或密码有误，请检查后重新输入哦~');</script>") ;
                return;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
